import { defineStore } from 'pinia';
import axios from '@/plugins/axios';
import { ElMessage } from 'element-plus';

export interface Question {
  id: number;
  title: string;
  content: string;
  type: 'SINGLE_CHOICE' | 'TRUE_FALSE' | 'TEXT';
  options: string[]; // 单选题选项，JSON 格式，判断题和大题可为空
  answer: string;
  status: 'DRAFT' | 'PUBLISHED';
  createTime: string;
}

export interface QuestionCreateRequest {
  title: string;
  content: string;
  type: 'SINGLE_CHOICE' | 'TRUE_FALSE' | 'TEXT';
  options: string[];
  answer: string;
}

export interface QuestionUpdateRequest extends QuestionCreateRequest {
  id: number;
  status: 'DRAFT' | 'PUBLISHED';
}

export const useQuestionStore = defineStore('question', {
  state: () => ({
    questions: [] as Question[],
  }),

  actions: {
    // 获取所有试题
    async fetchQuestions() {
      try {
        const response = await axios.get('/api/question');
        if (response.data.code === 200) {
          this.questions = response.data.data;
          return this.questions;
        }
        throw new Error(response.data.message || '获取试题列表失败');
      } catch (error: any) {
        ElMessage.error(error.message || '获取试题列表失败');
        throw error;
      }
    },

    // 根据状态获取试题
    async fetchQuestionsByStatus(status: string) {
      try {
        const response = await axios.get(`/api/question/status/${status}`);
        if (response.data.code === 200) {
          return response.data.data;
        }
        throw new Error(response.data.message || '获取试题列表失败');
      } catch (error: any) {
        ElMessage.error(error.message || '获取试题列表失败');
        throw error;
      }
    },

    // 获取单个试题详情
    async fetchQuestionById(id: number) {
      try {
        const response = await axios.get(`/api/question/${id}`);
        if (response.data.code === 200) {
          return response.data.data;
        }
        throw new Error(response.data.message || '获取试题详情失败');
      } catch (error: any) {
        ElMessage.error(error.message || '获取试题详情失败');
        throw error;
      }
    },

    // 创建试题
    async createQuestion(data: QuestionCreateRequest) {
      try {
        const response = await axios.post('/api/question', data);
        if (response.data.code === 200) {
          this.questions.push(response.data.data);
          ElMessage.success('创建试题成功');
          return response.data.data;
        }
        throw new Error(response.data.message || '创建试题失败');
      } catch (error: any) {
        ElMessage.error(error.message || '创建试题失败');
        throw error;
      }
    },

    // 更新试题
    async updateQuestion(data: QuestionUpdateRequest) {
      try {
        const response = await axios.put('/api/question', data);
        if (response.data.code === 200) {
          const index = this.questions.findIndex(q => q.id === data.id);
          if (index !== -1) {
            this.questions[index] = response.data.data;
          }
          ElMessage.success('更新试题成功');
          return response.data.data;
        }
        throw new Error(response.data.message || '更新试题失败');
      } catch (error: any) {
        ElMessage.error(error.message || '更新试题失败');
        throw error;
      }
    },

    // 在 actions 中添加
    async publishQuestion(id: number) {
      try {
        const response = await axios.post(`/api/question/publish/${id}`);
        if (response.data.code === 200) {
          const index = this.questions.findIndex(q => q.id === id);
          if (index !== -1) {
            this.questions[index] = response.data.data;
          }
          ElMessage.success('发布试题成功');
          return response.data.data;
        }
        throw new Error(response.data.message || '发布试题失败');
      } catch (error: any) {
        ElMessage.error(error.message || '发布试题失败');
        throw error;
      }
    },

    // 删除试题
    async deleteQuestion(id: number) {
      try {
        const response = await axios.delete(`/api/question/${id}`);
        if (response.data.code === 200) {
          this.questions = this.questions.filter(q => q.id !== id);
          ElMessage.success('删除试题成功');
          return response.data.data;
        }
        throw new Error(response.data.message || '删除试题失败');
      } catch (error: any) {
        ElMessage.error(error.message || '删除试题失败');
        throw error;
      }
    },
  },
});